<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/26
 * Time: 16:18
 */

namespace app\admin\controller;

use app\model\Role as RoleModel;
use app\model\Users;

class Role
{
    public function addRole(){
        if(request()->isPost()){
            $data = request()->only('role_name,role_info');
            $RoleModel = new RoleModel();
            $check_role = $RoleModel->check($data['role_name']);
            if($check_role != null) return res(0,'已存在该角色名称');
            $data['role_add_time'] = time();
            $add_res = $RoleModel->add($data);
            if($add_res){
                return res(1,'添加成功');
            }
            return res(0,'添加失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function getRole(){
        if(request()->isPost()){
            $RoleModel = new RoleModel();
            $data_res = $RoleModel->getAll();
            return res(1,'',$data_res);
        }
        return res(0,MSG_NOT_POST);
    }
    public function delRole(){
        if(request()->isPost()){
            $role_id = input('role_id');
            $RoleModel = new RoleModel();
            $check_role = $RoleModel->getById($role_id);
            if(!$check_role) return res(0,'不存在该角色');
            $UserModel = new Users();
            $check_user = $UserModel->checkRole($role_id);
            if($check_user > 0) return res(0,'无法删除，有' . $check_user . "个人是该角色");
            $res = $check_role->delete();
            if($res) return res(1,'删除成功');
            return res(0,'删除失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function editRole(){
        if(request()->isPost()){
            $data = request()->only('role_name,role_info');
            $role_id = input('role_id');
            $RoleModel = new RoleModel();
            if(isset($data['role_name'])){
                $check_name = $RoleModel->check($data['role_name']);
                if($check_name){
                    return res(0,'已存在该角色名');
                }
            }
            $res = $RoleModel->edit($role_id,$data);
            if($res){
                return res(1,'修改成功');
            }
            return res(0,'修改失败');
        }
        return res(0,MSG_NOT_POST);
    }

}